package org.potiorb.demo.server;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.potiorb.demo.generated.Teste;
import org.potiorb.demo.generated.TesteHelper;
import org.potiorb.demo.generated.TestePOAReflexive;
import org.potiorb.orb.ORB;
import org.potiorb.orb.giop.GIOPMessageReply;
import org.potiorb.orb.giop.GIOPMessageRequest;

public class TesteImplReflexive extends TestePOAReflexive{

	@Override
	public List<Object> executeBefore(String firstMethod, String secondMethod,
			String objectReferenceFirst, String objectReferenceSecond, List<Object> firstMethodParams, List<Object> secondMethodParams) {
		try {
			/**
			 * Pegando os parametros do primeiro método
			 */
			Class[] paramFirstMethod = new Class[firstMethodParams.size()];
			for (int i=0; i< firstMethodParams.size(); i++){
				paramFirstMethod[i]= firstMethodParams.get(i).getClass();
			}
			
			/**
			 * Pegando os paramentros do segundo método
			 */
			Class[] paramSecondMethod = new Class[secondMethodParams.size()];
			for (int i=0; i< secondMethodParams.size(); i++){
				paramSecondMethod[i]= secondMethodParams.get(i).getClass();
			}
			
			/**
			 * Pegando os métodos a serem a ser executados
			 */
			Method classFirstMethod = Class.forName(firstMethod).getMethod(firstMethod, paramFirstMethod);
			Method classSecondMethod = Class.forName(secondMethod).getMethod(secondMethod, paramSecondMethod);
			
			/**
			 * Pegando a referencia do objeto no ORB
			 */
			ORB orb = ORB.init();
			org.potiorb.orb.Object obj = orb.string_to_object(objectReferenceSecond);
			Teste teste = TesteHelper.narrow(obj);
			
			/**
			 * Invocando os métodos
			 */
			classFirstMethod.invoke(teste, paramFirstMethod);
			classSecondMethod.invoke(teste, paramSecondMethod);
					
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
				
		return null;
}



	@Override
	public List<Object> executeAfter(String firstMethod, String secondMethod,
			String objectReference, String objectReferenceSecond) {
		return null;
	}

	@Override
	public void simples_hello(String texto) {
		System.out.println("Hello World, from " + texto);		
	}



	@Override
	public GIOPMessageReply _invoke(GIOPMessageRequest msg) {
		// TODO Auto-generated method stub
		return null;
	}

}
